package com.kb.order.controller;

import com.kb.order.pojo.Order;
import com.kb.order.pojo.ShopInfo;
import com.kb.order.service.IOrderService;
import com.kb.order.service.IShopInfoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("car")
public class OrderCarController {
    @Autowired
    IShopInfoService infoService;

    /**
     * 查看购物车
     *
     */
    @RequestMapping("lookCar")
    public Map<Integer, ShopInfo> lookCar(){
        System.out.println("查看购物车，打印购物车信息");
        Map<Integer, ShopInfo> infoMap = infoService.lookCar();
        return infoMap;
    }

    /**
     * 添加商品到购物车
     * 传入商品id查询商品 及其所在店铺
     *
     * @return
     */
    @RequestMapping("joinCar")
    public String joinCar(Integer id){
        System.out.println("查询商品店铺开始");
        //查询到商品 放入购物车
        infoService.joinCar(id);
        return "1";
    }

    /**
     * 购物车商品加一
     * 当食品数量超出限购量时，不加一
     */
    @RequestMapping("plusFoodNum")
    public String plusFoodNum(Integer id){
        //对商品加一
        try {
            infoService.plusFoodNum(id);
            return "1";
        }catch (Exception e){
            return "2";
        }
    }
    /**
     * 购物车商品减一
     * 当食品数量为减少到0时移除食品
     */
    @RequestMapping("loseFoodNum")
    public String loseFoodNum(Integer id){
        //对商品减一
        try {
            infoService.loseFoodNum(id);
            return "1";
        }catch (Exception e){
            return "2";
        }
    }
    /**
     * 购物车商品删除
     */
    @RequestMapping("delCarFood")
    public String delCarFood(Integer id){
        System.out.println("删除商品开始");
        try {
            infoService.delCarFood(id);
            return "1";
        }catch (Exception e){
            e.printStackTrace();
            return "2";
        }
    }
    /**
     * 购物车商品结算
     * 判断食品库存
     * 生成订单
     */
    private Object foodId[];
    @RequestMapping("jsTotal")
    public String jsTotal(@RequestParam(value = "foodId[]") Object[] foodId){
        this.foodId = foodId;
        for (Object o : foodId) {
            System.out.println(o);
        }
        return "1";
    }
    private IOrderService orderService;
    /**
     * 结算购物车时跳转页面传参数
     */
    @RequestMapping("jsOrder")
    public String jsJump(Order order,Integer addressId){
        for (Object o : foodId) {
            System.out.println("数组中："+o);
        }
        infoService.addOrder(order,this.foodId,addressId);
        return "1";
    }
}
